package com.example.base.job;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.Map;

/**
 * @Classname JobConfig
 * @Description TODO
 * @Date 2023/9/25 11:15
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Configuration
public class JobConfig implements ApplicationContextAware {

    private Map<String,Object> classObject;

    private ApplicationContext applicationContext ;

    public Map<String,Object> getClassObject(){
        return this.classObject;
    }


    public Object getJobObjectByClassName(String className){
        if(classObject==null){
            return null;
        }
        Collection<Object> objects=classObject.values();
        for (Object object : objects) {
            if(object.getClass().getName().equals(className)){
                return object;
            }
        }
        return null;
    }

    @PostConstruct
    public void setUp() {
        // 根据注解获取bean ,key is bean name ,value is bean object
        Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(Job.class);
        this.classObject=map;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
